﻿using System;
using System.Web.Mvc;
using BrettM.Web.Hosting;
using BrettM.Web.Restful;
using Microsoft.Practices.Unity;

namespace BrettM.BootStrappers
{
    public class RestfulControllerFactoryBootStrapper
        : IBootStrapper
    {

        public void OnStart(IUnityContainer container, PropertyBag bag)
        {
            bag.Add("PreviousControllerFactory",  ControllerBuilder.Current.GetControllerFactory() );
            ControllerBuilder.Current.SetControllerFactory(new RestControllerFactory(container));
        }

        public void OnStop(IUnityContainer container, PropertyBag bag)
        {
            var previousFactory = bag["PreviousControllerFactory"] as IControllerFactory;
            if (previousFactory != null)
            {
                ControllerBuilder.Current.SetControllerFactory(previousFactory);
            }
        }

        public void Dispose()
        {
        }
    }
}
